import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBColor/YBColor.dart';
import 'dart:io';

import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBTextStyle/YBTextStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBUtils/YBBaseUtils/YBCallPhoneUtils.dart';

class YBHomePageFourDetailWebPage extends StatefulWidget {

  final String webStr;

  const YBHomePageFourDetailWebPage({Key key, this.webStr}) : super(key: key);

  @override
  _YBHomePageFourDetailWebPageState createState() => _YBHomePageFourDetailWebPageState(webStr:webStr);
}

class _YBHomePageFourDetailWebPageState extends State<YBHomePageFourDetailWebPage> {

  final String webStr;

  _YBHomePageFourDetailWebPageState({this.webStr});

  String _titleStr = "";
  
  WebViewController _controller;
  
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
  }

  @override
  void dispose() {
    EasyLoading.dismiss();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: YBColor.white,
      child: Scaffold(
        appBar: AppBar(
          elevation: 0,
          title:Container(
            child: Text(_titleStr,style: YBTextStyle.TextStyle30313320Medium,),
          ),
        ),
        body:WebView(
          initialUrl: webStr,
          onPageFinished:(url){
            EasyLoading.dismiss();
            _controller.evaluateJavascript("document.title").then((value){
              setState(() {
                _titleStr = value;
                print("-----------$value--------");
              });
            });
          },
          onWebViewCreated: (controller){
            _controller = controller;
          },
          
          onPageStarted: (url){
            EasyLoading.show(status:"读取中...");
          },
          javascriptChannels: <JavascriptChannel>[
            JavascriptChannel(name: "commtjToast", onMessageReceived:(JavascriptMessage message){
              print("object--${message.message}---");
              YBCallPhoneUtils().getCallPhoneUtils("tel:${message.message}");
            }),
          ].toSet(),
          javascriptMode: JavascriptMode.unrestricted,
        ),
      ),
    );
  }
}
